import GA_Test.GAData;
import 多无人机架次.TimeCount;

import java.util.ArrayList;
import java.util.Random;

public class Test03 {
    public static void main(String[] args) {
        ArrayList<Integer> truck = new ArrayList<>();
        ArrayList<Integer> drone = new ArrayList<>();
        truck.add(1);truck.add(2);truck.add(3);truck.add(4);truck.add(5);truck.add(8);
        drone.add(1);drone.add(6);drone.add(2);drone.add(3);drone.add(7);drone.add(5);
        exchange(truck,drone);
    }
private static void exchange(ArrayList<Integer> truck_route, ArrayList<Integer> drone_route) {
        double best = TimeCount.timeCount(truck_route,drone_route);//将当前传进来的这个组合的时间设为最好的
        ArrayList<Integer> message = new ArrayList<>();
        for (int t = 0; t < truck_route.size(); t++) {
        for (int d = 0; d < drone_route.size(); d++) {
        if (truck_route.get(t) == drone_route.get(d)) {
        message.add(t);
        message.add(d);
        message.add(truck_route.get(t));
        }
        }
        }
        int truck_off1_position = message.get(0); //车辆访问路径中 无人机第一次起飞的位置
        int drone_off1_position = message.get(1);// 无人机路径中  无人机第一次起飞的位置
        int off1 = message.get(2);// 起飞的客户点
        int truck_down1_position = message.get(3);//车辆访问路径中 无人机第一次降落的位置
        int drone_down1_position = message.get(4);//无人机路径中 无人机第一次降落的位置
        int down1 = message.get(5);//降落的客户点
        int truck_off2_position = 0;
        int drone_off2_position = 0;
        int off2 = 0;
        int truck_down2_position = 0;
        int drone_down2_position = 0;
        int down2 = 0;
        if (message.size() > 6) {
        truck_off2_position = message.get(6);
        drone_off2_position = message.get(7);
        off2 = message.get(8);
//            System.out.println(truck_route);
//            System.out.println(drone_route);
//            System.out.println(message);
        truck_down2_position = message.get(9);
        drone_down2_position = message.get(10);
        down2 = message.get(11);
        }
//        if (message.size() == 6) {//只有一个架次  单架次用随机撞
//        if(truck_down1_position-truck_off1_position>1) {//当无人机只有一个架次时  这种交换必须在 起飞点和降落点之间有节点的前提下进行
//        for(int k = 0;k<4;k++) {//这个循环是为了 多进行几次交换
//        Random r = new Random();
//        int drone_point = r.nextInt(drone_route.size()-2)+1;
//        int truck_point = r.nextInt(truck_down1_position-truck_off1_position-1)+truck_off1_position+1;
//        //把上面拿到的这两个点进行交换
//        truck_route.set(truck_point,drone_route.get(drone_point));
//        drone_route.set(drone_point,truck_route.get(truck_point));//交换完毕
//        double time = TimeCount.timeCount(truck_route,drone_route);
//        if(time<best){
//        best = time;
//        }else{
//        truck_route.set(truck_point,drone_route.get(drone_point));
//        drone_route.set(drone_point,truck_route.get(truck_point));//交换回去
//        }
//        }
//        }
//        } else { //有两个架次
        //把无人机与车辆的访问顾客点分别提取出来
        ArrayList<Integer> truck_service = new ArrayList<>();
        ArrayList<Integer> drone_service = new ArrayList<>();
        for(int k = drone_off1_position+1;k<drone_down1_position;k++){
        drone_service.add(drone_route.get(k));
        }
        for(int k = drone_off2_position+1;k<drone_down2_position;k++){
        drone_service.add(drone_route.get(k));
        }
        for(int k = 0;k<truck_off1_position;k++){
        truck_service.add(truck_route.get(k));
        }
        for(int k = truck_off1_position+1;k<truck_down1_position;k++){
        truck_service.add(truck_route.get(k));
        }
        for(int k = truck_down1_position+1;k<truck_off2_position;k++){
        truck_service.add(truck_route.get(k));
        }
        for(int k = truck_off2_position+1;k<truck_down2_position;k++){
        truck_service.add(truck_route.get(k));
        }
        for(int k = truck_down2_position+1;k<truck_route.size();k++){
        truck_service.add(truck_route.get(k));
        }
        //随机从车辆访问顾客中挑一个点，从无人机访问顾客中挑一个点进行交换
        Random random =  new Random();
        for(int k = 0;k<10;k++) {
        int truck_point = truck_service.get(random.nextInt(truck_service.size()));
        int truck_point_position = 0;
        int drone_point = drone_service.get(random.nextInt(drone_service.size()));
        int drone_point_position = 0;
        //找到这个车辆节点与无人机节点的所在位置    然后进行对应交换
        for(int n = 0;n<truck_route.size();n++){
        if(truck_point==truck_route.get(n)){
        truck_point_position = n;
        }
        }
        for(int n = 0;n<drone_route.size();n++){
        if(drone_point == drone_route.get(n)){
        drone_point_position = n;
        }
        }
        truck_route.set(truck_point_position,drone_point);
        drone_route.set(drone_point_position,truck_point);//交换完成
        //进行交换后的约束检查
        if(isok(truck_route,drone_route)==true){//如果交换后满足约束
        double time = TimeCount.timeCount(truck_route,drone_route);//计算交换后的时间
              if(time<best){
                best = time;
                }else{
                 truck_route.set(truck_point_position,truck_point);
                   drone_route.set(drone_point_position,drone_point);//交换回去
                }
         }else{ //不满足约束的话  将车辆路径进行还原 继续进行交换
        truck_route.set(truck_point_position,truck_point);
        drone_route.set(drone_point_position,drone_point);//交换回去
        }

        }
        }

private static boolean isok(ArrayList<Integer> truck, ArrayList<Integer> drone) {
        boolean flag = true;

        ArrayList<Integer> message = new ArrayList<>();
        for (int t = 0; t < truck.size(); t++) {
        for (int d = 0; d < drone.size(); d++) {
        if (truck.get(t) == drone.get(d)) {
        message.add(t);
        message.add(d);
        message.add(truck.get(t));
        }
        }
        }
        int truck_off1_position = message.get(0); //车辆访问路径中 无人机第一次起飞的位置
        int drone_off1_position = message.get(1);// 无人机路径中  无人机第一次起飞的位置
        int off1 = message.get(2);// 起飞的客户点
        int truck_down1_position = message.get(3);//车辆访问路径中 无人机第一次降落的位置
        int drone_down1_position = message.get(4);//无人机路径中 无人机第一次降落的位置
        int down1 = message.get(5);//降落的客户点
        int truck_off2_position = 0;
        int drone_off2_position = 0;
        int off2 = 0;
        int truck_down2_position = 0;
        int drone_down2_position = 0;
        int down2 = 0;
        if (message.size() > 6) {
        truck_off2_position = message.get(6);
        drone_off2_position = message.get(7);
        off2 = message.get(8);
        truck_down2_position = message.get(9);
        drone_down2_position = message.get(10);
        down2 = message.get(11);
        }
        double drone_distance1 =0; int cap1 = 0;
        double drone_distance2 =0; int cap2 = 0;
        for(int k =drone_off1_position;k<drone_down1_position;k++){
        drone_distance1 += GAData.disCollection[drone.get(k)][drone.get(k+1)]; //无人机第一架次距离计算
        }
        for(int k = drone_off1_position+1;k<drone_down1_position;k++){//无人机第一架次载重计算
        cap1 += GAData.cusInfo[k].demand;
        }
        if(drone_distance1>120 || cap1>40){
        flag = false;
        }
        if(drone_off2_position!=0){
        for(int k =drone_off2_position;k<drone_down2_position;k++){
        drone_distance2 += GAData.disCollection[drone.get(k)][drone.get(k+1)]; //无人机第一架次距离计算
        }
        for(int k = drone_off2_position+1;k<drone_down2_position;k++){//无人机第二架次载重计算
        cap2 += GAData.cusInfo[k].demand;
        }
        }
        if(drone_distance2>120 || cap2>40){
        flag = false;
        }
        return flag;
        }
        }